package com.reebake.ideal.desensitize.core;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.reebake.ideal.desensitize.properties.DesensitizeProperties;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 默认脱敏处理器
 */
public class DefaultDesensitizeHandler implements DesensitizeHandler {
    @Autowired
    private DesensitizeProperties desensitizeProperties;

    @Override
    public String desensitize(String value, String desensitizeType) {
        if (StrUtil.isBlank(value)) {
            return value;
        }

        DesensitizeProperties.DesensitizeRule desensitizeRule = desensitizeProperties.getRules().get(desensitizeType);
        Assert.notNull(desensitizeRule, "规则配置错误: {}", desensitizeType);
        return value.replaceAll(desensitizeRule.getRegex(), desensitizeRule.getReplacement());
    }
}
